﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SlotJoint.CApPlaneManipulator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.SlotJoint;

internal class CApPlaneManipulator
{
  private TxManipulator _planeManipulator;
  private double _width;
  private double _height;
  private ITxObject _object;
  private static readonly TxColor _planeManipulatorColor = new TxColor((byte) 203, (byte) 233, (byte) 254, 0.3);
  private TxColor _planeBorderManipulatorColor = new TxColor((byte) 134, (byte) 134, (byte) 174);

  public CApPlaneManipulator(
    ITxLocatableObject objectContext,
    TxTransformation locationTransformation)
  {
    this._object = (ITxObject) objectContext;
    this.CalculateManipulatorSize(locationTransformation);
    this.CreateManipulator(locationTransformation);
  }

  private void CalculateManipulatorSize(TxTransformation locationTransformation)
  {
    this._width = this._height = 5000.0;
    if (this._object == null)
      return;
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(this._object);
    TxBoundingBoxCalculator boundingBoxCalculator = new TxBoundingBoxCalculator(txObjectList);
    boundingBoxCalculator.Orientation = locationTransformation;
    try
    {
      ArrayList boundingBoxVertices = boundingBoxCalculator.CalculateVisibleMinimalPlaneBoundingBoxVertices();
      boundingBoxCalculator.Orientation = new TxTransformation(locationTransformation.RotationRPY_XYZ, (TxTransformation.TxRotationType) 1);
      if (boundingBoxCalculator.CalculateVisibleMinimalBoundingBox().IsEmpty || boundingBoxVertices[0] == boundingBoxVertices[1])
        return;
      this._width = CApSlotJointUtils.CalculateDistance(boundingBoxVertices[1] as TxVector, boundingBoxVertices[2] as TxVector);
      this._height = CApSlotJointUtils.CalculateDistance(boundingBoxVertices[0] as TxVector, boundingBoxVertices[1] as TxVector);
    }
    catch (TxException ex)
    {
      TxApplication.LogWriter.WriteExceptionLine((Exception) ex);
    }
  }

  private void CreateManipulator(TxTransformation location)
  {
    TxManipulatorPlaneElementData planeElementData = new TxManipulatorPlaneElementData(new TxTransformation(), this._width, this._height);
    ((TxManipulatorElementData) planeElementData).Color = CApPlaneManipulator._planeManipulatorColor;
    ((TxManipulatorElementData) planeElementData).Pickable = false;
    ((TxManipulatorElementData) planeElementData).AlwaysOnTop = false;
    TxManipulatorPolylineElementData polylineElementData = new TxManipulatorPolylineElementData(new TxTransformation(), new ArrayList()
    {
      (object) new TxVector(this._width / 2.0, this._height / 2.0, 0.0),
      (object) new TxVector(-this._width / 2.0, this._height / 2.0, 0.0),
      (object) new TxVector(-this._width / 2.0, -this._height / 2.0, 0.0),
      (object) new TxVector(this._width / 2.0, -this._height / 2.0, 0.0),
      (object) new TxVector(this._width / 2.0, this._height / 2.0, 0.0)
    });
    ((TxManipulatorElementData) polylineElementData).Color = this._planeBorderManipulatorColor;
    polylineElementData.LineWidth = 2.0;
    ((TxManipulatorElementData) polylineElementData).Pickable = false;
    ((TxManipulatorElementData) polylineElementData).AlwaysOnTop = false;
    TxManipulatorCreationData manipulatorCreationData = new TxManipulatorCreationData("SlotJointPlaneManipulator", location);
    manipulatorCreationData.InsertElementData((TxManipulatorElementData) planeElementData);
    manipulatorCreationData.InsertElementData((TxManipulatorElementData) polylineElementData);
    this._planeManipulator = TxApplication.ActiveDocument.PhysicalRoot.CreateManipulator(manipulatorCreationData);
    this._planeManipulator.Display();
  }

  public void DestroyManipulator()
  {
    if (!TxManipulator.op_Inequality(this._planeManipulator, (ITxObject) null))
      return;
    this._planeManipulator.Delete();
    this._planeManipulator = (TxManipulator) null;
  }
}
